package lunchy.mvc.views;

import java.util.ArrayList;
import java.util.List;

import lunchy.data.interfaces.ILunchyMenuGroup;
import lunchy.data.interfaces.ILunchyMenuItem;
import lunchy.mvc.controllers.CLunchyMenuEditController;
import lunchy.main.CLunchyApplication;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.TableItem;

public class CLunchyMenuEditView
{
	private Shell shellMenuEdit;
	private CLunchyMenuEditController controller;
	private Table menuEditTable;
	private Button changeActiveButton;
	private Button addMenuItemButton;
	
	private final Integer SEL_UNKNOWN = 0;
	private final Integer SEL_ACTIVE = 1;
	private final Integer SEL_NONACTIVE = 2;
	
	
	public CLunchyMenuEditView()
	{
		controller = new CLunchyMenuEditController();
		
		shellMenuEdit = new Shell(CLunchyApplication.getInstance().display, SWT.CLOSE | SWT.TITLE | SWT.MIN | SWT.APPLICATION_MODAL);
		shellMenuEdit.setMinimumSize(new Point(200, 200));
		shellMenuEdit.setText("LunchY | Menu Edit");
		shellMenuEdit.setSize(580, 480);
		
		initUI();
		
		shellMenuEdit.open();
        
        while (!shellMenuEdit.isDisposed())
        {
        	if (!CLunchyApplication.getInstance().display.readAndDispatch())
        		CLunchyApplication.getInstance().display.sleep();
        }
	}

	public void initUI()
	{
		menuEditTable = new Table(shellMenuEdit, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
		menuEditTable.setBounds(10, 10, 560, 398);
		menuEditTable.setHeaderVisible(true);
		menuEditTable.setLinesVisible(true);
		
		TableColumn idTableColoumn = new TableColumn(menuEditTable, SWT.NONE);
		idTableColoumn.setWidth(50);
		idTableColoumn.setText("ID");
		
		TableColumn nameTableColumn = new TableColumn(menuEditTable, SWT.NONE);
		nameTableColumn.setWidth(340);
		nameTableColumn.setText("Название");
		
		TableColumn priceTableColumn = new TableColumn(menuEditTable, SWT.NONE);
		priceTableColumn.setWidth(91);
		priceTableColumn.setText("Цена");
		
		TableColumn activeTableColumn = new TableColumn(menuEditTable, SWT.NONE);
		activeTableColumn.setWidth(90);
		activeTableColumn.setText("Активность");
		
		changeActiveButton = new Button(shellMenuEdit, SWT.NONE);
		changeActiveButton.setBounds(10, 414, 147, 28);
		changeActiveButton.setText("Активировать");
		changeActiveButton.setEnabled(false);
		changeActiveButton.addMouseListener(
			new MouseListener()
			{
				public void mouseUp(MouseEvent e)
				{
					Integer sel = whatIsSelected();
					
					TableItem[] selItems = menuEditTable.getSelection();

					if(sel == SEL_ACTIVE)
					{
						for (TableItem tableItem : selItems)
						{
							controller.deactivateItem(tableItem.getText(0));
						}
					}
					else if(sel == SEL_NONACTIVE)
					{
						for (TableItem tableItem : selItems)
						{
							controller.activateItem(tableItem.getText(0));
						}
					}
					
					fillTable();
				}
				
				public void mouseDown(MouseEvent e)
				{

				}
				
				public void mouseDoubleClick(MouseEvent e)
				{

				}
			}
		);
		
		Button addMenuGroupButton = new Button(shellMenuEdit, SWT.NONE);
		addMenuGroupButton.setBounds(163, 414, 143, 28);
		addMenuGroupButton.setText("Добавить группу");
		addMenuGroupButton.addMouseListener(
			new MouseListener()
			{
				public void mouseUp(MouseEvent e)
				{
					controller.addGroup();
					fillTable();
				}

				public void mouseDown(MouseEvent e)
				{

				}

				public void mouseDoubleClick(MouseEvent e)
				{

				}
			}
		);
		
		addMenuItemButton = new Button(shellMenuEdit, SWT.NONE);
		addMenuItemButton.setBounds(312, 414, 143, 28);
		addMenuItemButton.setText("Добавить блюдо");
		addMenuItemButton.setEnabled(false);
		addMenuItemButton.addMouseListener(
			new MouseListener()
			{
				public void mouseUp(MouseEvent e)
				{
					TableItem[] selItems = menuEditTable.getSelection();
					
					if(selItems.length == 1)
					{
						if(selItems[0].getData("group").toString().equals("true"))
						{
							CLunchyApplication.getInstance().debug("[CLunchyMenuEditView] adding item to group with ID = " + selItems[0].getText(0));
							controller.addItem(selItems[0].getText(0));
							fillTable();
						}
					}
					
				}

				public void mouseDown(MouseEvent e)
				{

				}

				public void mouseDoubleClick(MouseEvent e)
				{

				}
			}
		);
		
		Button menuSaveButton = new Button(shellMenuEdit, SWT.NONE);
		menuSaveButton.setBounds(461, 414, 109, 28);
		menuSaveButton.setText("Сохранить");
		menuSaveButton.addMouseListener(
			new MouseListener()
			{
				public void mouseUp(MouseEvent e)
				{
					controller.saveMenu();
					shellMenuEdit.dispose();
				}
	
				public void mouseDown(MouseEvent e)
				{
	
				}
	
				public void mouseDoubleClick(MouseEvent e)
				{
	
				}
			}
		);
		
		menuEditTable.addSelectionListener(
			new SelectionListener()
			{
				public void widgetSelected(SelectionEvent e)
				{
					Integer sel = whatIsSelected();
					
					if(sel == SEL_ACTIVE)
					{
						changeActiveButton.setEnabled(true);
						changeActiveButton.setText("Деактивировать");
					}
					else if(sel == SEL_NONACTIVE)
					{
						changeActiveButton.setEnabled(true);
						changeActiveButton.setText("Активировать");
					}
					else
						changeActiveButton.setEnabled(false);

					addMenuItemButton.setEnabled(false);
					
					TableItem[] selItems = menuEditTable.getSelection();
					
					if(selItems.length == 1)
					{
						if(selItems[0].getData("group").toString().equals("true"))
							addMenuItemButton.setEnabled(true);
					}
				}

				public void widgetDefaultSelected(SelectionEvent e)
				{

				}
			}
		);
		
		menuEditTable.addMouseListener(
			new MouseListener()
			{
				public void mouseUp(MouseEvent e)
				{
					if(e.button == 3)
					{
						TableItem[] selItems = menuEditTable.getSelection();
						
						if(selItems.length == 1)
						{
							MessageBox askDelete = new MessageBox(shellMenuEdit, SWT.ICON_QUESTION | SWT.YES | SWT.NO);
							askDelete.setMessage("Вы действительно хотите удалить данный элемент?");
						    
							int rc = askDelete.open();
						    
							if(rc == SWT.YES)
							{
								if(selItems[0].getData("group").toString().equals("true"))
									controller.deleteGroup(selItems[0].getText());
								else
									controller.deleteItem(selItems[0].getText());
								
								fillTable();
							}
						}
					}
				}

				public void mouseDown(MouseEvent e)
				{

				}

				public void mouseDoubleClick(MouseEvent e)
				{
					TableItem[] selItems = menuEditTable.getSelection();
					
					if(selItems.length == 1)
					{
						if(selItems[0].getData("group").toString().equals("true"))
						{
							CLunchyApplication.getInstance().debug("[CLunchyMenuEditView] editing group with ID = " + selItems[0].getText(0));
							controller.editGroup(selItems[0].getText(0));
						}
						else
						{
							CLunchyApplication.getInstance().debug("[CLunchyMenuEditView] editing item with ID = " + selItems[0].getText(0));
							controller.editItem(selItems[0].getText(0));
						}

						fillTable();
					}
				}
			}
		);
		
		fillTable();
	}
	
	private void fillTable()
	{
		menuEditTable.removeAll();

		for (ILunchyMenuGroup currentGroup : controller.getMenuGroups())
		{
			////Group
		    TableItem tableGroup = new TableItem(menuEditTable, SWT.NONE);
		    tableGroup.setText(new String[] {currentGroup.getId().toString(), currentGroup.getName()});
		    tableGroup.setBackground(new Color(null, 100, 100, 100));
		    tableGroup.setForeground(new Color(null, 255, 255, 255));
		    tableGroup.setData("group", "true");
			
		    for (ILunchyMenuItem currentItem : currentGroup.getItems())
		    {
		    	////Item
		    	TableItem tableItem = new TableItem(menuEditTable, SWT.NONE);
		    	tableItem.setText(new String[] {currentItem.getId().toString(), currentItem.getName(), String.format("%d руб.", currentItem.getPrice()), currentItem.isActive() ? "Да" : "Нет"});
		    	tableItem.setData("group", "false");
		    	
		    	if(!currentItem.isActive())
		    		tableItem.setForeground(new Color(null, 170, 170, 170));
		    }
		}
	}
	
	private Integer whatIsSelected()
	{
		TableItem[] selItems = menuEditTable.getSelection();
		
		boolean activeSelected = false, unactiveSelected = false;
		
		for (TableItem tableItem : selItems)
		{
			if(tableItem.getData("group").toString().equals("false"))
			{
				if(!activeSelected && controller.isActiveItem(tableItem.getText(0)))
					activeSelected = true;
				else if(!unactiveSelected && !controller.isActiveItem(tableItem.getText(0)))
					unactiveSelected = true;
			}
			else
			{
				activeSelected = unactiveSelected = true;
				break;
			}
		}
		
		if(activeSelected && !unactiveSelected)
			return SEL_ACTIVE;
		else if(!activeSelected && unactiveSelected)
			return SEL_NONACTIVE;
		else
			return SEL_UNKNOWN;
	}
}
